/*
 * @Author: TC
 * @Date: 2025-06-20 16:32:02
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-28 16:05:35
 */

import { app } from "db://assets/FrameWork/App";
import { ViewLogic } from "./ViewLogic";
import { nsBulletInterface } from "../../../Game/Battle/Bullet/BulletInterface";
import { myGame } from "../../../MyGame";
import { v3, Vec3 } from "cc";

export class BulletMotionLogic {
    private _mapBullet: Map<string, nsBulletInterface.IBulletPath> = null;
    constructor() {
        this._mapBullet = new Map();
        app.tickMgr.add(this);
    }
    /**
     * 添加子弹
     * @param iBulletPath
     */
    public addBullet(iBulletPath: nsBulletInterface.IBulletPath): void {
        this._mapBullet.set(iBulletPath.nodeBullet.uuid, iBulletPath);
        this._initBullet(iBulletPath);
        console.log("添加子弹", iBulletPath.nodeBullet.position, iBulletPath.nodeBullet.angle, iBulletPath);
        myGame.battleMgr.bulletLayer.addChild(iBulletPath.nodeBullet);
    }

    /**
     * 添加子弹
     * @param iBulletPath
     */
    private _initBullet(iBulletPath: nsBulletInterface.IBulletPath): void {
        const targetPos = v3().set(iBulletPath.initTartetPos);
        //初始化角度
        let angle = ViewLogic.getAngleByVec(targetPos.x - iBulletPath.nodeBullet.x, targetPos.y - iBulletPath.nodeBullet.y);
        if (iBulletPath.bullet.autoAngle) {
            iBulletPath.bulletMove.setAngel(angle);
        }
        const dir = ViewLogic.getVecByAngle(angle);
        //初始速度
        const speed = iBulletPath.bullet.cfg.speed;
        iBulletPath.bullet.move.setVelocity(dir.x, dir.y, speed);
    }

    public tick(dt: number): void {}

    public release(): void {
        app.tickMgr.remove(this);
        this._mapBullet.clear();
        this._mapBullet = null;
    }
}
